package com.ruoyi.his.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.his.domain.OdsHospitalAll;
import com.ruoyi.his.domain.vo.HFtotleVo;
import com.ruoyi.his.mapper.OdsHospitalAllMapper;
import com.ruoyi.system.domain.HisLicensedOverWarning;
import com.ruoyi.system.domain.HisVerifyOverdueWarning;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.his.mapper.HisPunishMapper;
import com.ruoyi.his.domain.HisPunish;
import com.ruoyi.his.service.IHisPunishService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Alan
 * @date 2024-03-19
 */
@Service
public class HisPunishServiceImpl implements IHisPunishService
{
    @Autowired
    private HisPunishMapper hisPunishMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserPostMapper sysUserPostMapper;
    @Autowired
    private SysPostMapper sysPostMapper;
    @Autowired
    private HisEngineAnalysisMapper hisEngineAnalysisMapper;
    @Autowired
    private OdsHospitalAllMapper odsHospitalAllMapper;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public HisPunish selectHisPunishById(Long id)
    {
        return hisPunishMapper.selectHisPunishById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param hisPunish 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HisPunish> selectHisPunishList(HisPunish hisPunish)
    {
        List<HisPunish> hisPunishes = hisPunishMapper.selectHisPunishList(hisPunish);
        for (HisPunish punish:hisPunishes){
            if(punish.getPunishUid()!=null){
                SysUser sysUser = sysUserMapper.selectUserById(punish.getPunishUid());
                if(StringUtils.isNotBlank(sysUser.getDeptId())){
                    SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
                    punish.setDeptName(sysDept.getDeptName());
                }
                List<SysPost> sysPosts = sysPostMapper.selectPostsByUserName(sysUser.getUserName());
                String collect = sysPosts.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
                punish.setPostName(collect);
                punish.setRealName(sysUser.getNickName());
            }
            if(StringUtils.isNotBlank(punish.getRegisterNumber())){
                OdsHospitalAll odsHospitalAll = odsHospitalAllMapper.selectRegisterNumber(punish.getRegisterNumber());
                punish.setInstitutionName(odsHospitalAll.getInstitutionName());
            }
        }
        return hisPunishes;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param hisPunish 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHisPunish(HisPunish hisPunish)
    {
        for (String str:hisPunish.getIds()){
            HisPunish punish=new HisPunish();
            BeanUtils.copyProperties(hisPunish,punish);
            punish.setWarnId(Long.parseLong(str));
            punish.setWarnType(1);
            SysUser user = SecurityUtils.getLoginUser().getUser();
            punish.setPunishUid(user.getUserId());
            punish.setPunishTime(new Date());
            hisPunishMapper.insertHisPunish(punish);
        }
        return 1;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param hisPunish 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHisPunish(HisPunish hisPunish)
    {
        return hisPunishMapper.updateHisPunish(hisPunish);
    }

    /**
     * 回复是否判断是插入还是更新
     * @param hisPunish 【请填写功能名称】
     * @return
     */
    @Override
    public int InertOrUpdata(HisPunish hisPunish)
    {
        HisPunish hisPunishById = hisPunishMapper.selectById(hisPunish.getId());

        if (hisPunishById != null) {
            if (StringUtils.isEmpty(hisPunishById.getReContent())&&StringUtils.isEmpty(hisPunishById.getReFile())){
                return hisPunishMapper.updateHisPunish(hisPunish);
            }
            HisPunish newHisPunish = new HisPunish();
            BeanUtils.copyProperties(hisPunishById, newHisPunish);
            newHisPunish.setId(null);
            newHisPunish.setReContent(hisPunish.getReContent());
            newHisPunish.setReTime(hisPunish.getReTime());
            newHisPunish.setReFile(hisPunish.getReFile());
            return hisPunishMapper.insert(newHisPunish);

        }
        return 0;
    }

    @Override
    public List<HFtotleVo> getTotleList(HisPunish hisPunish) {
        return hisPunishMapper.getTotleListInfo(hisPunish);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHisPunishByIds(Long[] ids)
    {
        return hisPunishMapper.deleteHisPunishByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHisPunishById(Long id)
    {
        return hisPunishMapper.deleteHisPunishById(id);
    }

    @Override
    public List<HisVerifyOverdueWarning> myVerifyWarningInfo(HisVerifyOverdueWarning hisVerifyOverdueWarning) {
        return hisPunishMapper.selectMyVerifyWarningInfo(hisVerifyOverdueWarning);
    }

    @Override
    public List<HisLicensedOverWarning> myHisLicensedOverWarningList(HisLicensedOverWarning hisLicensedOverWarning) {


        return hisPunishMapper.myHisLicensedOverWarningInfo(hisLicensedOverWarning);
    }
}
